<?php
/**
 * common model file Created by PhpStorm.
 * User: wumengmeng
 * Date: 2019/10/16
 * Time: 17:34
 */

namespace HiCommon\Model;

class Discover extends Base
{
    
    protected $table = 'discover';
    
    protected $guarded = [];
    
    //public $timestamps = false;
    
    /**
     * 应该被调整为日期的属性
     *
     * @var array
     */
    protected $dates = [
    ];
    
    /**
     * 获取状态对应的名称
     *
     * @return array
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function getStateAttribute($value)
    {
        switch ($value)
        {
            case 0:
                $res = ['id'=>0,'name'=>'草稿'];
                break;
            case 1:
                $res = ['id'=>1,'name'=>'待审核'];
                break;
            case 2:
                $res = ['id'=>2,'name'=>'通过'];
                break;
            case 3:
                $res = ['id'=>3,'name'=>'拒绝'];
                break;
            default:
                $res = ['id'=>-1,'name'=>'未知'];
        }
        return $res;
    }

        public function getPidAttribute($value)
    {
        switch ($value)
        {
            case 1:
                $res = ['id'=>1,'name'=>'保养专区'];
                break;
            case 2:
                $res = ['id'=>2,'name'=>'检测专区'];
                break;
            case 3:
                $res = ['id'=>3,'name'=>'新闻专区'];
                break;
            default:
                $res = ['id'=>-1,'name'=>'未知'];
        }
        return $res;
    }
    

    public function getTypeAttribute($value)
    {
        switch ($value)
        {
            case 1:
                $res = ['id'=>1,'name'=>'前段发布'];
                break;
            case 2:
                $res = ['id'=>2,'name'=>'后端发布'];
                break;
            default:
                $res = ['id'=>-1,'name'=>'未知'];
        }
        return $res;
    }

    public function getIssueAttribute($value)
    {
        switch ($value)
        {
            case 1:
                $res = ['id'=>1,'name'=>'已发布'];
                break;
            case 2:
                $res = ['id'=>2,'name'=>'未发布'];
                break;
            default:
                $res = ['id'=>-1,'name'=>'未知'];
        }
        return $res;
    }
    
        /**
         * 获取图片可用链接
         *
         * @return array
         * @author wumengmeng <wu_mengmeng@foxmail.com>
         */
        public function getImgAttribute($value)
        {
            $arr_data = [
              'src'=>$value,
              'full_src'=>local_full_url($value),
            ];
            return $arr_data;
        }
        
        /**
         * 扩展信息
         *
         * @return array
         * @author wumengmeng <wu_mengmeng@foxmail.com>
         */
        public function getExtendAttribute()
        {
            $s_created_at = $this->created_at;
            $arr_data = [
              'created_at'=>ymd_friendly_date($s_created_at),
            ];

            return $arr_data;
        }



    //管理前段用户表
    public function user()
    {
        return $this->belongsTo(Users::class, 'author_id')->select('id','nickname','head_img','integral_all');
    }

    //管理前段用户表
    public function admin()
    {
        return $this->belongsTo(Admins::class, 'author_id')->select('id','username');
    }

    //管理前段用户表
    public function shop_user()
    {
        return $this->belongsTo(ShopUser::class, 'author_id')->select('id','username');
    }



    //点赞的统计
    public function countZan(){
        return $this->hasMany(DiscoverZan::class, 'link_id')->where('type',1);
    }

    //评论的统计
    public function countPing(){
        return $this->hasMany(DiscoverApprise::class, 'discover_id');
    }

    //评论的统计
    public function is_read(){
        return $this->hasMany(DiscoverApprise::class, 'discover_id')->where('is_read',1);
    }

    //点赞统计
    public function zan()
    {
        return $this->hasMany(DiscoverZan::class, 'link_id')
            ->select('id','user_id','link_id')
            ->where('type',1);
    }

    //关联收藏
    public function is_collect(){
        return $this->hasMany(UserCollect::class, 'x_id');
    }










}